package com.IOC.beans;

import java.util.Map;

/**
 * 系统启动时调用，初始化所有单例的bean实例，因为系统启动时不会有并发问题，所以此时争抢锁资源情况较少，不再需要双重检测（因为此时bean实例一定还没创建）
 */
public class PreBuildBeanFactory extends DefaultBeanFactory{

    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(PreBuildBeanFactory.class);

    public void preInstantiateSingletons() throws Exception {
        synchronized (this.beanDefinitionMap) {
            for(Map.Entry<String, BeanDefinition> entry : this.beanDefinitionMap.entrySet()) {
                String beanName = entry.getKey();
                BeanDefinition bd = entry.getValue();
                if(bd.isSingleton()) {
                    // 缓存所有单例bean
                    this.getBean(beanName);
                    if(log.isDebugEnabled()) {
                        // 打印debug日志
                        log.debug("preInstantiate: beanName=" + beanName + " " + bd);
                    }
                }
            }
        }

    }

}
